package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Menu;
import org.kjtc.entity.Page;
import org.kjtc.service.MenuService;
import org.kjtc.util.Const;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MenuController extends BaseController{

	@Resource(name = "menuService")
	private MenuService menuService;

	@RequestMapping(value = "/searchMenu")
	@ResponseBody
	public Map<String, Object> searchMenu(Menu menu, String page, String rows, String LayerType){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息查询开始");

		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		// 获取所有设备数据
		List<Menu> menulist = null;
		try {
			if (LayerType != null && !LayerType.isEmpty()) {
				menu.setLayer(Integer.parseInt(LayerType));
			}
			menulist = menuService.getMenuPage(menu, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息查询异常" + e.getMessage());
			return map;
		}

		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("menuList", menulist);

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息查询结束");
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveMenu")
	public Map<String, Object> saveMenu(Menu menu){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息添加开始");
		menu.setCreateUser(this.getUesr().getUserID());
		menu.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = menuService.insertMenu(menu);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_MENUNAME, null);
				map.put("result", "Success");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息添加成功");
			} else {
				map.put("result", "NotFound");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息添加失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息添加异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息添加结束");
		return map;
	}

	@RequestMapping(value = "/updateMenu")
	@ResponseBody
	public Map<String, Object> updateMenu(Menu menu) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息修改开始");
		menu.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = menuService.updateMenu(menu);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_MENUNAME, null);
				map.put("result", "Success");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息修改成功");
			} else {
				map.put("result", "NotFound");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息修改异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息修改结束");
		return map;
	}

	@RequestMapping(value = "/deleteMenu")
	@ResponseBody
	public Object deleteMenu(String resourcesID) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息删除开始");
		String[] resourcesIDList = resourcesID.split(",");
		Menu menu = new Menu();
		menu.setResourcesIDList(resourcesIDList);
		menu.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = menuService.deleteMenu(menu);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_MENUNAME, null);
				map.put("result", "Success");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息删除成功");
			} else {
				map.put("result", "NotFound");
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "菜单信息删除结束");
		return map;
	}
}
